package com.xcd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xcd.reggie.common.BaseContext;
import com.xcd.reggie.common.R;
import com.xcd.reggie.entity.ShoppingCart;
import com.xcd.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("添加的到购物车的菜品或套餐信息:   {}",shoppingCart.toString() );

        //设置当前购物车的用户的id
        Long currentID = BaseContext.getCurrentID();
        shoppingCart.setUserId(currentID);

        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(currentID != null,ShoppingCart::getUserId,currentID);
        //判断是菜品或或者套餐
        if (dishId != null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);

        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart serviceOne = shoppingCartService.getOne(queryWrapper);

        if (serviceOne != null){
            Integer number = serviceOne.getNumber();
            serviceOne.setNumber(number + 1);
            shoppingCartService.updateById(serviceOne);
        }else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            serviceOne = shoppingCart;
        }

        return R.success(serviceOne);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        Long currentID = BaseContext.getCurrentID();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(currentID != null,ShoppingCart::getUserId,currentID);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentID());
        shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("清空成功");
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("sub（）---》：{}",shoppingCart);
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentID());
        Integer number;


        if (dishId != null){
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
            ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
             number = one.getNumber();
            one.setNumber(--number);
            shoppingCartService.updateById(one);
        }
        else {
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
            number = one.getNumber();
            one.setNumber(--number);
            shoppingCartService.updateById(one);
        }
        if (number == 0){
            shoppingCartService.remove(shoppingCartLambdaQueryWrapper);

        }

        return R.success("sub成功");
    }
}
